/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical.wizard.spray;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.model.CropProtection;
import nl.cloudfarming.client.planning.tactical.TacticalPlanningModule;
import org.openide.util.NbBundle;

/**
 * Panel which will present a list of cropprotection Pesticides. A user can choose at least one or more and continue the wizzard.
 * @author Merijn Zengers
 */
public final class CropProtectionPanel extends JPanel {

    private JScrollPane pane = new JScrollPane();
    private JTable cropProtectionTable = new JTable();
    private DefaultTableModel availableTableModel;
    public static final int USAGE_DATE_COLUMN = 2;
    private final String HEADER_PESTICIDE = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.crop_header_pesticide");
    private final String PCT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.crop_header_pct");
    private final String TITLE = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.title");
    private final String DATE_FORMAT = NbBundle.getMessage(TacticalPlanningModule.class, "field_plan.wizard.spray.crop_protection_list_panel.date_format");
    
    /** Creates new form PlanningVisualPanel2 */
    public CropProtectionPanel() {
        initComponents();
    }

    @Override
    public String getName() {
        return TITLE;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * 
     */
    private void initComponents() {
        setLayout(new MigLayout("wrap 1"));
        cropProtectionTable.setRowSelectionAllowed(true);


        pane.setViewportView(cropProtectionTable);
        this.add(pane);
    }

    public void setSelectedCropProtections(List<CropProtection> selectedCropProtections) {

        assert (!selectedCropProtections.isEmpty());

        //Only if the table is empty
        if (availableTableModel == null) {
            String[][] displayList = new String[selectedCropProtections.size()][3];

            int i = 0;
            for (CropProtection cropProtection : selectedCropProtections) {
                displayList[i][0] = cropProtection.getName();

                SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
                String formattedStartDate = sdf.format(cropProtection.getLastUsage());
                displayList[i][1] = formattedStartDate;
                i++;
            }

            availableTableModel = new DefaultTableModel(displayList,
                    new String[]{
                        HEADER_PESTICIDE, PCT
                    });
            cropProtectionTable.setModel(availableTableModel);
        }
    }

    /**
     * Get the selected crop protection agents from the table.
     * @return String[rowindex][colindex] contains the selected cropprotections
     * rowindex: The number of selected rows in the displayed table
     * colindex: Fixed USAGE_DATE_COLUMN
     */
    public String[][] getSelectedCropProtections() {
        String[][] selectedValues = new String[cropProtectionTable.getSelectedRows().length][USAGE_DATE_COLUMN];
        int i = 0;
        for (int index : cropProtectionTable.getSelectedRows()) {
            selectedValues[i][0] = (String) availableTableModel.getValueAt(index, 0);
            selectedValues[i][1] = (String) availableTableModel.getValueAt(index, 1);
            i++;
        }
        return selectedValues;
    }
}